package com.aloda.ojuser.controller;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.ojcommon.constant.ErrorCode;
import com.aloda.ojcommon.domain.result.PageResult;
import com.aloda.ojcommon.domain.result.Result;
import com.aloda.ojcommon.utils.EncryptionUtil;
import com.aloda.ojcommon.anno.AuthCheck;
import com.aloda.ojuser.pojo.dto.UserAddDTO;
import com.aloda.ojuser.pojo.dto.UserEditDTO;
import com.aloda.ojuser.pojo.dto.UserLoginDTO;
import com.aloda.ojuser.pojo.dto.UserPageDTO;
import com.aloda.ojuser.pojo.entity.User;
import com.aloda.ojcommon.domain.enums.UserRoleEnum;
import com.aloda.ojuser.pojo.vo.UserInfoVO;
import com.aloda.ojuser.pojo.vo.UserLoginVO;
import com.aloda.ojuser.pojo.vo.UserOnlineVO;
import com.aloda.ojuser.pojo.vo.UserPageVO;
import com.aloda.ojuser.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author aloda
 * @Date 2025/4/20 15:25
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户通过邮箱登录
     * @param userLoginDTO 用户登录数据传输类
     * @return UserLoginVO
     * @throws Exception 解密异常
     */
    @PostMapping("/login")
    public Result<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO) throws Exception {
        if (BeanUtil.isEmpty(userLoginDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        log.info("用户登录:{}", userLoginDTO);
        String decrypt = EncryptionUtil.decrypt(userLoginDTO.getEmail());
        userLoginDTO.setEmail(decrypt);
        UserLoginVO userLoginVO = userService.loginByEmail(userLoginDTO);
        return Result.success(userLoginVO);
    }

    /**
     * 用户退出
     * @return
     */
    @PostMapping("/logout")
    public Result userLogout(HttpServletRequest request) {
        userService.userLogout(request);
        return Result.success();
    }

    /**
     * 分页查询用户
     * @param userPageDTO
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @PostMapping("/page")
    public Result<PageResult<UserPageVO>> pageQuery(@RequestBody UserPageDTO userPageDTO) {
        if (BeanUtil.isEmpty(userPageDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        PageResult<UserPageVO> pageResult = userService.pageQuery(userPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增用户
     * @param userAddDTO
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @PostMapping
    public Result<Long> addUser(@RequestBody UserAddDTO userAddDTO) {
        Long id = userService.addUser(userAddDTO);
        log.info("新增用户:{}",id);
        return Result.success(id);
    }

    /**
     * 编辑用户
     * @param userEditDTO
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @PutMapping
    public Result editUser(@RequestBody UserEditDTO userEditDTO) {
        log.info("编辑用户:{}",userEditDTO.getId());
        userService.editUser(userEditDTO);
        return Result.success();
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @GetMapping("/{id}")
    public Result<UserInfoVO> getUserInfoById(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(BeanUtil.copyProperties(user, UserInfoVO.class));
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @DeleteMapping
    public Result deleteUser(@RequestBody List<Long> ids) {
        log.info("批量删除用户：{}", ids);
        userService.deleteUserByIds(ids);
        return Result.success();
    }

    /**
     * 获取所有在线用户
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @GetMapping("/online")
    public Result<List<UserOnlineVO>> getAllOnlineUser() {
        List<UserOnlineVO> list = userService.getAllOnlineUser();
        return Result.success(list);
    }

    /**
     * 下线用户
     * @param token
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @DeleteMapping("/online/{token}")
    public Result offlineUser(@PathVariable String token) {
        userService.offlineUser(token);
        return Result.success();
    }
}
